package com.sinopac.action.GroupManagement;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.sinopac.actionCore.ActionCore;
import com.sinopac.entity.GroupManagement.MainAccountVO;
import com.sinopac.entity.GroupManagement.RelateAccountVO;
import com.sinopac.model.GroupManagement.GroupManagementModel;


@Namespace("/GroupManagement")
@ParentPackage("struts-default")
public class GroupManagementAction extends ActionCore{

	private static final long serialVersionUID = 1L;

	//收從後端傳來的訊息參數
	private String errorMessage;
	//收後端傳來的 是否為集團戶token判斷
	private boolean is_group_token;
	
	//主帳戶table用變數
	private String custId;
	private String custName;
	private String aoCode;
	private String tokenESN;
	private String otpStatus;
	private String mainAcc;
	private List<MainAccountVO> mainAccVO;
	
	//關聯帳戶table用變數
	private String relateCustId;
	private String relateCustName;
	private String relateAoCode;
	private String relateOtpStatus;
	private String setMainAcct;
	private String moveOut;
	private List<RelateAccountVO> relateAccVO;
	
	//新增關聯帳戶用變數
	private String newRelateCustId;
	//使用者角色
	private String userRoles;
	//是否已申請Token
	private String token_str;

	@Action(value="index",results={@Result(name="login",location="/form/GroupManagement/groupManagement.jsp")})
	public String execute(){
		userRoles = (String)session.get(USER_ROLES);
		
		if(StringUtils.isNotBlank(custId)){
			session.put(CustomersID, custId);
		}
		GroupManagementModel gm = new GroupManagementModel();
//		Boolean error_msg = gm.chkJobQs((String)session.get(CustomersID));		
		mainAccVO = gm.queryMainAccountList((String)session.get(CustomersID));
		relateAccVO = gm.queryRelateAccountList((String)session.get(CustomersID));
		return LOGIN;
	}
	
	@Action(value="remove",results={@Result(name="login",location="/form/GroupManagement/groupManagement.jsp"),
									@Result(name="success",type="chain",location="index")})
	public String remove() throws SQLException{
		GroupManagementModel gm = new GroupManagementModel();			
		
//		String custId_a = "";

//		custId_a = (String) session.get(CustomersID);
		
//		System.out.println("custId_a:"+custId_a);
		System.out.println("relateCustId:"+relateCustId);
		
		gm.deleteRelateAccountList(relateCustId); //先註解掉 以免他執行。
		
		return SUCCESS;
	}
	
	@Action(value="update",results={@Result(name="login",location="/form/GroupManagement/groupManagement.jsp"),
									@Result(name="success",type="chain",location="index")})
	public String update(){
		GroupManagementModel gm = new GroupManagementModel();			
		
		String custId_a = "";

		custId_a = (String) session.get(CustomersID);
		
		System.out.println("custId_a:"+custId_a);
		System.out.println("relateCustId:"+relateCustId);
		
		gm.updateRelateAccountList(relateCustId, custId_a);
		
		return SUCCESS;
	}
	
	@Action(value="addNew",results={@Result(name="login",location="/form/GroupManagement/groupManagement.jsp"),
									@Result(name="success",type="chain",location="index")})
	public String addNew(){
		GroupManagementModel gm = new GroupManagementModel();		
		
		String custId_a = "";

		custId_a = (String) session.get(CustomersID);
		
		System.out.println("newRelateCustId:"+newRelateCustId);
		System.out.println("custId_a:"+custId_a);
		
		// 客戶ID前面18位未滿的話補空白
		StringBuffer custIdMix = new StringBuffer();
		if(newRelateCustId.length()<23){
			String[] strlist = newRelateCustId.split("-");
			custIdMix.append(strlist[0].toString());
			for(int i=1; i<=18-strlist[0].length(); i++){
				custIdMix.append(" ");
			}
			custIdMix.append("-" + strlist[1].toString() + "-" + strlist[2].toString());
			gm.addNewRelateAccountList(custIdMix.toString(),custId_a);
		}else{
			gm.addNewRelateAccountList(newRelateCustId,custId_a);
		}
		
		errorMessage = gm.getErrorMessage();
		System.out.println("errorMessage = gm.getErrorMessage();===="+errorMessage);
		
		
		return SUCCESS;
	}
	
	//收從後端傳來的訊息參數
	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}
	
	//收後端傳來的 是否為集團戶token判斷
	public boolean isIs_group_token() {
		return is_group_token;
	}

	public void setIs_group_token(boolean is_group_token) {
		this.is_group_token = is_group_token;
	}

	//主帳戶table用POJO	
	public String getCustId() {
		return custId;
	}

	public void setCustId(String custId) {
		this.custId = custId;
	}

	public String getCustName() {
		return custName;
	}

	public void setCustName(String custName) {
		this.custName = custName;
	}

	public String getAoCode() {
		return aoCode;
	}

	public void setAoCode(String aoCode) {
		this.aoCode = aoCode;
	}

	public String getTokenESN() {
		return tokenESN;
	}

	public void setTokenESN(String tokenESN) {
		this.tokenESN = tokenESN;
	}

	public String getOtpStatus() {
		return otpStatus;
	}

	public void setOtpStatus(String otpStatus) {
		this.otpStatus = otpStatus;
	}

	public String getMainAcc() {
		return mainAcc;
	}

	public void setMainAcc(String mainAcc) {
		this.mainAcc = mainAcc;
	}
	
	public List<MainAccountVO> getMainAccVO() {
		return mainAccVO;
	}

	public void setMainAccVO(List<MainAccountVO> mainAccVO) {
		this.mainAccVO = mainAccVO;
	}

	
	//關聯帳戶table用POJO	
	public String getRelateCustId() {
		return relateCustId;
	}

	public void setRelateCustId(String relateCustId) {
		this.relateCustId = relateCustId;
	}

	public String getRelateCustName() {
		return relateCustName;
	}

	public void setRelateCustName(String relateCustName) {
		this.relateCustName = relateCustName;
	}

	public String getRelateAoCode() {
		return relateAoCode;
	}

	public void setRelateAoCode(String relateAoCode) {
		this.relateAoCode = relateAoCode;
	}

	public String getRelateOtpStatus() {
		return relateOtpStatus;
	}

	public void setRelateOtpStatus(String relateOtpStatus) {
		this.relateOtpStatus = relateOtpStatus;
	}
	
	public List<RelateAccountVO> getRelateAccVO() {
		return relateAccVO;
	}

	public void setRelateAccVO(List<RelateAccountVO> relateAccVO) {
		this.relateAccVO = relateAccVO;
	}

	public String getSetMainAcct() {
		return setMainAcct;
	}

	public void setSetMainAcct(String setMainAcct) {
		this.setMainAcct = setMainAcct;
	}

	public String getMoveOut() {
		return moveOut;
	}

	public void setMoveOut(String moveOut) {
		this.moveOut = moveOut;
	}
		
	public String getToken_str() {
		return token_str;
	}

	public void setToken_str(String token_str) {
		this.token_str = token_str;
	}

	//新增關聯帳戶　用POJO	
	public String getNewRelateCustId() {
		return newRelateCustId;
	}

	public void setNewRelateCustId(String newRelateCustId) {
		this.newRelateCustId = newRelateCustId;
	}

	public String getUserRoles() {
		return userRoles;
	}

	public void setUserRoles(String userRoles) {
		this.userRoles = userRoles;
	}	
}
